package com.vmj.classfile.entity;

import com.vmj.classfile.entity.attribute.*;

import java.util.List;

/**
 * Created by yangshoujun on 2017/8/5 下午11:29.
 */
public class MemberInfo {

    int accessFlags;
    int nameIndex;
    int descriptorIndex;
    String descriptor;
    String name;
    List<Attribute> attributes;

    public CodeAttribute getCodeAttribute() {
        for(Attribute attribute : attributes) {
            if(attribute instanceof CodeAttribute) {
                return (CodeAttribute) attribute;
            }
        }
        return null;
    }

    public ConstantValueAttribute getConstantValueAttribute() {
        for(Attribute attribute : this.getAttributes()) {
            if(attribute instanceof ConstantValueAttribute) {
                return (ConstantValueAttribute) attribute;
            }
        }
        return null;
    }

    public ExceptionsAttribute getExceptionsAttribute() {
        for(Attribute attribute : this.getAttributes()) {
            if(attribute instanceof ExceptionsAttribute) {
                return (ExceptionsAttribute) attribute;
            }
        }
        return null;
    }

    public byte[] getRuntimeVisibleAnnotationsAttributeData() {
        return getUnparsedAttributeData("RuntimeVisibleAnnotations");
    }

    public byte[] getRuntimeVisibleParameterAnnotationsAttributeData() {
        return getUnparsedAttributeData("RuntimeVisibleParameterAnnotationsAttribute");
    }

    public byte[] getAnnotationDefaultAttributeData() {
        return getUnparsedAttributeData("AnnotationDefault");
    }

    public byte[] getUnparsedAttributeData(String name) {
        if(attributes == null || attributes.isEmpty()) {
            return null;
        }
        for(int i = 0;i < attributes.size();i++) {
            Attribute attribute = attributes.get(i);
            if(attribute instanceof UnparsedAttribute) {
                UnparsedAttribute unparsedAttribute = (UnparsedAttribute) attribute;
                if(unparsedAttribute.getName() != null && unparsedAttribute.getName().equals(name)) {
                    return ((UnparsedAttribute) attribute).getInfo();
                }
            }
        }
        return null;
    }

    public int getAccessFlags() {
        return accessFlags;
    }

    public int getNameIndex() {
        return nameIndex;
    }

    public int getDescriptorIndex() {
        return descriptorIndex;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAccessFlags(int accessFlags) {
        this.accessFlags = accessFlags;
    }

    public void setNameIndex(int nameIndex) {
        this.nameIndex = nameIndex;
    }

    public void setDescriptorIndex(int descriptorIndex) {
        this.descriptorIndex = descriptorIndex;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescriptor() {
        return descriptor;
    }

    public void setDescriptor(String descriptor) {
        this.descriptor = descriptor;
    }
}
